#include "guard.h"

Guard::Guard(void)
{
	this->health = 50;
	this->attackPower = 3;
	this->speed = GUARDS_SPEED;
	//set to be more than moveTimeInterval so that when
	//move is called the first if statement will execute
	this->moveTime = 1;
	this->moveTimeInterval = 0;
	//set to move to the right initually
	this->moveTimeDirection = 3;
	//set all the function pointer to the appropriate functions.
	this->direcFuncPtrs[0] = &guardUp;
	this->direcFuncPtrs[1] = &guardUpRight;
	this->direcFuncPtrs[2] = &guardRight;
	this->direcFuncPtrs[3] = &guardDownRight;
	this->direcFuncPtrs[4] = &guardDown;
	this->direcFuncPtrs[5] = &guardDownLeft;
	this->direcFuncPtrs[6] = &guardLeft;
	this->direcFuncPtrs[7] = &guardUpLeft;
}
void Guard::draw(sf::RenderWindow &window)
{
	this->move(window);
	this->Sprite.SetPosition(this->xPos, this->yPos);
	window.Draw(this->Sprite);
}
//moves the guard in a 1 of 8 directions for 
//a random time interval between 0.1-1.0 seconds
void Guard::move(sf::RenderWindow &window)
{
	int movedir = 7;
	int direc[8];
	std::vector<int> directions;
	for(int i = 0; i < 8; i++)
	{
		direc[i] = i;
	}
	if( this->moveTime >= this->moveTimeInterval )
	{
		this->moveTime = 0;
		this->moveTimeInterval = sf::Randomizer::Random(1.0f, 1.5f);
		if(this->xPos < 0)
		{
			direc[5] = -1;
			direc[6] = -1;
			direc[7] = -1;
		}
		if(this->xPos > 800)
		{
			direc[1] = -1;
			direc[2] = -1;
			direc[3] = -1;
		}
		if(this->yPos < 0)
		{
			direc[1] = -1;
			direc[2] = -1;
			direc[7] = -1;
		}
		if(this->yPos > 600)
		{
			direc[4] = -1;
			direc[5] = -1;
			direc[6] = -1;
		}
		for(int i = 0; i < 8; i++)
		{
			if(direc[i] != -1)
			{
				directions.push_back(direc[i]);
			}
		}
		this->moveTimeDirection = directions.at(sf::Randomizer::Random(0,(int)(directions.size() - 1)));
	}
	this->moveTime += window.GetFrameTime();
	//directions 1=up 2=upright 3=right 4=downright... (continues clockwise)
	//calls the move direction function.
	direcFuncPtrs[moveTimeDirection](this, 0);
}
void guardUp(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->yPos -= guard->speed;
}
void guardUpRight(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->yPos -= guard->speed;
	guard->xPos += guard->speed;
}
void guardRight(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->xPos += guard->speed;
}
void guardDownRight(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->yPos += guard->speed;
	guard->xPos += guard->speed;
}
void guardDown(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->yPos += guard->speed;
}
void guardDownLeft(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->yPos += guard->speed;
	guard->xPos -= guard->speed;
}
void guardLeft(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->xPos -= guard->speed;
}
void guardUpLeft(void *object, void *extraData)
{
	Guard* guard = static_cast<Guard*>(object);
	guard->yPos -= guard->speed;
	guard->xPos -= guard->speed;
}